package patient

import (
	"api-gateway/basic/config"
	proto "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/pkg"
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// Register 患者注册 //已测试
func Register(c *gin.Context) {
	var req request.PatientRegisterReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.Register(ctx, &proto.RegisterRequest{
		Phone:    req.Phone,
		Password: req.Password,
		SmsCode:  req.SmsCode,
	})
	if err != nil {
		handleGrpcError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":   200,
		"msg":    resp.Message,
		"userId": resp.UserId,
	})
}

// Login 患者登录 //已测试
func Login(c *gin.Context) {
	var req request.PatientLoginReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.Login(ctx, &proto.LoginRequest{
		Phone:    req.Phone,
		Password: req.Password,
	})
	if err != nil {
		handleGrpcError(c, err)
		return
	}
	token, err := pkg.GetJwtToken(int(resp.UserId))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "生成 token 失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":  200,
		"msg":   resp.Message,
		"token": token,
		"user": gin.H{
			"userId": resp.UserId,
			"phone":  resp.Phone,
		},
	})
}

// ForgotPassword 发送重置密码验证码
func ForgotPassword(c *gin.Context) {
	var req request.ForgotPasswordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.SendResetCode(ctx, &proto.SendResetCodeRequest{
		Phone: req.Phone,
	})
	if err != nil {
		handleGrpcError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

// ResetPassword 重置密码
func ResetPassword(c *gin.Context) {
	var req request.ResetPasswordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.ResetPassword(ctx, &proto.ResetPasswordRequest{
		Phone:    req.Phone,
		SmsCode:  req.SmsCode,
		Password: req.NewPassword,
	})
	if err != nil {
		handleGrpcError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

// Logout 用户登出
//func Logout(c *gin.Context) {
//	// 从上下文获取用户ID
//	userID := c.GetInt64("userId")
//	if userID == 0 {
//		c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": "用户未登录"})
//		return
//	}
//
//	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
//	defer cancel()
//
//	// RPC调用
//	resp, err := config.PatientClient.Logout(ctx, &proto.LogoutRequest{
//		UserId: userID,
//	})
//	if err != nil {
//		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "登出失败: " + err.Error()})
//		return
//	}
//
//	c.JSON(http.StatusOK, gin.H{
//		"code": 200,
//		"msg":  resp.Message,
//	})
//}

// handleGrpcError 处理gRPC错误
func handleGrpcError(c *gin.Context, err error) {
	c.JSON(http.StatusInternalServerError, gin.H{
		"code": 500,
		"msg":  "服务暂不可用，请稍后重试",
	})
}
